# _*_ coding:utf-8 _*_
"""
@Coding: utf-8
@Version: python3.8
@Project: appium
@File: Func.PY
@Date: 2022/05/23 22:40
@Author: jia
@Description: 常用函数
"""
import csv
import time
import os
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.common.multi_action import MultiAction
from selenium.common import NoSuchElementException


class Func:
    def __init__(self, web_driver):
        self.driver = web_driver

    def check_element_exist(self, element):

        source = self.driver.page_source

        if element in source:
            return True
        else:
            return False

    def is_element_exist(self, by, ele):
        flag = None
        try:
            if by == 'predicate':
                self.driver.find_element(AppiumBy.IOS_PREDICATE, ele)
            elif by == 'accessibility':
                self.driver.find_element(AppiumBy.ACCESSIBILITY_ID, ele)
            elif by == 'xpath':
                self.driver.find_element_by_xpath(ele)
            flag = True
        except NoSuchElementException as e:
            flag = False
        finally:
            return flag

    # 双指滑动
    def swipe(self, start_x, start_y, end_x, end_y, duration):
        return self.driver.swipe(start_x, start_y, end_x, end_y, duration)

    # 获取屏幕尺寸
    def get_screen_size(self):
        x = self.driver.get_window_size()['width']
        y = self.driver.get_window_size()['height']
        return x, y

    # 左划
    def swipe_left(self):
        size = self.get_screen_size()
        x1 = int(size[0] * 0.9)
        y1 = int(size[1] * 0.5)
        x2 = int(size[0] * 0.1)
        self.driver.swipe(x1, y1, x2, y1, 1000)

    # 上划
    def swipe_up(self):
        size = self.get_screen_size()
        x1 = int(size[0] * 0.5)
        y1 = int(size[1] * 0.95)
        y2 = int(size[1] * 0.35)
        self.driver.swipe(x1, y1, x1, y2, 1000)

    # 下划
    def swipe_down(self):
        size = self.get_screen_size()
        x1 = int(size[0] * 0.5)
        y1 = int(size[1] * 0.35)
        y2 = int(size[1] * 0.85)
        self.driver.swipe(x1, y1, x1, y2, 1000)

    # 右划
    def swipe_right(self):
        size = self.get_screen_size()
        y1 = int(size[1] * 0.5)
        x1 = int(size[0] * 0.25)
        x2 = int(size[0] * 0.95)
        self.driver.swipe(x1, y1, x2, y1, 1000)

    # 缩小
    def zoom_out(self):
        x, y = self.get_screen_size()
        action1 = TouchAction(self.driver)
        action2 = TouchAction(self.driver)
        pinch_action = MultiAction(self.driver)

        action1.press(x=x * 0.2, y=y * 0.2).wait(1000).move_to(x=x * 0.4, y=y * 0.4).wait(1000).release()
        action2.press(x=x * 0.8, y=y * 0.8).wait(1000).move_to(x=x * 0.6, y=y * 0.6).wait(1000).release()
        # mutiaction方法，ADD()添加对象，perform()执行
        pinch_action.add(action1, action2)
        pinch_action.perform()

    # 放大
    def zoom_in(self):
        x, y = self.get_screen_size()
        action1 = TouchAction(self.driver)
        action2 = TouchAction(self.driver)
        zoom_action = MultiAction(self.driver)

        action1.press(x=x * 0.4, y=y * 0.4).wait(1000).move_to(x=x * 0.2, y=y * 0.2).wait(1000).release()
        action2.press(x=x * 0.6, y=y * 0.6).wait(1000).move_to(x=x * 0.8, y=y * 0.8).wait(1000).release()

        zoom_action.add(action1, action2)
        zoom_action.perform()

    # 获取年-月-日 时：分：秒
    @staticmethod
    def get_now_time(self):
        now = time.strftime("%Y-%m-%d %H_%M_%S")
        return now

    # 保存截图
    def screenshots(self, save_path):
        now_time = time.strftime("%Y-%m-%d %H_%M_%S")
        # 获得当前文件所在目录绝对路径
        # currentPath = os.path.dirname(os.path.abspath(__file__))
        image_file = os.path.join(save_path + '%s.png' % now_time)
        self.driver.get_screenshot_as_file(image_file)

    # 获取CSV文件指定行的数据
    def get_csv_data(self, csv_file, line):
        with open(csv_file, 'r', encoding='utf-8-sig') as file:
            reader = csv.reader(file)
            for index, row in enumerate(reader):  # enumerate对一个可迭代对象输出下标和值，默认初始下标值从0开始
                if index == line:
                    return row


if __name__ == '__main__':
    from Common.driver_iphone import desired
    driver = desired()
    list_data = 'C:\\Users\\Administrator\\Desktop\\new.csv'
    f = Func(driver)
    f.get_csv_data(list_data, 5)
    for i in range(3):
        f.zoom_in()

    for i in range(3):
        f.zoom_out()
